package cn.lx.log.core.annotation;

import cn.lx.log.autoconfigure.LogProperties;
import cn.lx.log.core.util.MethodInvocationUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;


/**
 * 日志增强advice
 * @author wcy
 */
public class DynamicLogAdvice implements MethodInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(DynamicLogAdvice.class);

    private LogProperties logProperties;

    public DynamicLogAdvice() {
    }

    public DynamicLogAdvice(LogProperties logProperties) {
        this.logProperties = logProperties;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        logger.info("日志前置处理");
        Object object = null;
        String methodName = null;

        try {
            final long startMills = System.currentTimeMillis();
            Method method = MethodInvocationUtil.getActualMethod(methodInvocation);
            methodName = method.toString();

            object = methodInvocation.proceed();
        }catch (Exception e){
            logger.error("出现错误");
            throw e;
        }finally {
            logger.info("方法执行完成");
        }
        logger.info("日志返回");
        return object;
    }
}
